package top.anydata.lifeofcode.lifesystem.web.controller.admin.auth;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.anydata.lifeofcode.lifesystem.dto.sys.user.SysAuthPermissionDto;
import top.anydata.lifeofcode.lifesystem.dto.sys.user.SysAuthRoleDto;
import top.anydata.lifeofcode.lifesystem.dto.sys.user.SysAuthUserDto;
import top.anydata.lifeofcode.lifesystem.service.sys.user.IAuthRoleService;
import top.anydata.lifeofcode.lifesystem.service.sys.user.IAuthUserService;
import top.anydata.lifeofcode.lifesystem.web.responses.admin.ApiResponse;

import java.util.List;

/**
 * 功能描述: 权限控制器
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2024/2/1 2:24 PM
 */
@Slf4j
@RestController
@RequestMapping("/sys/api/access")
@AllArgsConstructor
public class AccessControlController {

    IAuthRoleService authRoleService;

    IAuthUserService authUserService;

    /**
     * 用户相关接口.
     * user/list 翻页查找用户列表.
     * user/add  添加用户
     * user/update 更新用户
     * user/delete 删除用户
     * user/ban 禁用用户
     * user/unban 启用用户
     * user/addRole 给用户添加角色
     * user/removeRole 删除用户角色
     */
    @GetMapping("/user/list")
    @PreAuthorize("hasPermission(#user,'sys::user::list')")
    public ResponseEntity<ApiResponse<Page<SysAuthUserDto>>> userList(int page, int size) {
        return ResponseEntity.ok(
                ApiResponse.success(authUserService.listUser(page, size))
        );
    }

    @PostMapping("/user/add")
    @PreAuthorize("hasPermission(#user,'sys::user::add')")
    public ResponseEntity<ApiResponse<String>> addUser(@RequestBody SysAuthUserDto sysAuthUserDto) {
        log.info("add user:{}", sysAuthUserDto);
        authUserService.addUser(sysAuthUserDto);
        return ResponseEntity.ok(
                ApiResponse.success("success")
        );
    }

    @DeleteMapping("/user/delete/{id}")
    @PreAuthorize("hasPermission(#user,'sys::user::delete')")
    public ResponseEntity<ApiResponse<String>> deleteUser(@PathVariable Integer id) {
        authUserService.deleteUserById(id);
        return ResponseEntity.ok(
                ApiResponse.success("success")
        );
    }

    @PostMapping("/user/update")
    @PreAuthorize("hasPermission(#user,'sys::user::update')")
    public ResponseEntity<ApiResponse<String>> updateUser(@RequestBody SysAuthUserDto sysAuthUserDto) {
        authUserService.updateUser(sysAuthUserDto);
        return ResponseEntity.ok(
                ApiResponse.success("success")
        );
    }

    @PostMapping("/user/ban/{id}")
    @PreAuthorize("hasPermission(#user,'sys::user::ban')")
    public ResponseEntity<ApiResponse<String>> banUser(@PathVariable Integer id) {
        authUserService.banUserById(id);
        return ResponseEntity.ok(
                ApiResponse.success("success")
        );
    }

    @PostMapping("/user/unban/{id}")
    @PreAuthorize("hasPermission(#user,'sys::user::unban')")
    public ResponseEntity<String> unbanUser(@PathVariable Integer id) {
        authUserService.unBanUserById(id);
        return ResponseEntity.ok("success");
    }

    @PostMapping("/user/addRole/{userId}/{roleId}")
    @PreAuthorize("hasPermission(#user,'sys::user::addRole')")
    public ResponseEntity<String> addRoleToUser(@PathVariable Integer userId, @PathVariable Integer roleId) {
        authUserService.addRoleToUser(userId, roleId);
        return ResponseEntity.ok("success");
    }

    @DeleteMapping("/user/removeRole/{userId}/{roleId}")
    @PreAuthorize("hasPermission(#user,'sys::user::removeRole')")
    public ResponseEntity<String> removeRoleFromUser(@PathVariable Integer userId, @PathVariable Integer roleId) {
        authUserService.removeRoleFromUser(userId, roleId);
        return ResponseEntity.ok("success");
    }


    /**
     * 角色相关接口
     * role/list 翻页查找角色列表
     * role/add 添加角色
     * role/update 更新角色
     * role/delete 删除角色
     * role/addPermission 给角色添加权限
     * role/removePermission 删除角色权限
     */
    @GetMapping("/role/list")
    @PreAuthorize("hasPermission(#user,'sys::role::list')")
    public ResponseEntity<ApiResponse<Page<SysAuthRoleDto>>> roleList(int page, int size) {
        return ResponseEntity.ok(
                ApiResponse.success(authRoleService.getRoleList(page, size))
        );
    }

    @PostMapping("/role/add")
    @PreAuthorize("hasPermission(#user,'sys::role::add')")
    public ResponseEntity<String> addRole(SysAuthRoleDto roleDto) {
        authRoleService.createRole(roleDto);
        return ResponseEntity.ok("success");
    }

    @PostMapping("/role/update")
    @PreAuthorize("hasPermission(#user,'sys::role::update')")
    public ResponseEntity<String> updateRole(SysAuthRoleDto roleDto) {
        authRoleService.updateRole(roleDto);
        return ResponseEntity.ok("success");
    }

    // 删除角色
    @DeleteMapping("/role/{id}")
    @PreAuthorize("hasPermission(#user,'sys::role::delete')")
    public ResponseEntity<Void> deleteRole(@PathVariable Integer id) {
        authRoleService.deleteRole(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
     * 权限相关接口
     * permission/list 翻页查找权限列表
     * permission/add 添加权限
     * permission/update 更新权限
     * permission/delete 删除权限
     */
    @GetMapping("/permission/list")
    @PreAuthorize("hasPermission(#user,'sys::permission::list')")
    public ResponseEntity<ApiResponse<Page<SysAuthPermissionDto>>> permissionList(int page, int size) {
        return ResponseEntity.ok(
                ApiResponse.success(authRoleService.getPermissionList(page, size))
        );
    }

    @PostMapping("/permission/add")
    @PreAuthorize("hasPermission(#user,'sys::permission::add')")
    public ResponseEntity<String> addPermission(SysAuthPermissionDto permissionDto) {
        authRoleService.createPermission(permissionDto);
        return ResponseEntity.ok("success");
    }

    @PostMapping("/permission/update")
    @PreAuthorize("hasPermission(#user,'sys::permission::update')")
    public ResponseEntity<String> updatePermission(SysAuthPermissionDto permissionDto) {
        authRoleService.updatePermission(permissionDto);
        return ResponseEntity.ok("success");
    }

    @DeleteMapping("/permission/{id}")
    @PreAuthorize("hasPermission(#user,'sys::permission::delete')")
    public ResponseEntity<Void> deletePermission(@PathVariable Integer id) {
        authRoleService.deletePermission(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/permission/getPermissionByUserId")
    @PreAuthorize("hasPermission(#user,'sys::permission::getPermissionByUserId')")
    public ResponseEntity<ApiResponse<List<SysAuthPermissionDto>>> getPermissionByUserId(int userId) {
        return ResponseEntity.ok(
                ApiResponse.success(authRoleService.getPermissionByUserId(userId))
        );
    }

}
